﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Box2D.XNA;
using ElementumForever.Utility;
using Microsoft.Xna.Framework.Input;
using LuaInterface;
using ElementumForever.Factory;
using ElementumForever.Interfaces;
using Microsoft.Xna.Framework.Graphics;
using ElementumForever.Decorators;
using ElementumForever.Entity.Scene;

namespace ElementumForever.Editer
{
    public class LuaEditerScene : ConcreteScene, IMouseControl
    {
        List<IMouseControl> _mouseListener;
        SelectionManager _selectionManager;

        //EntityList _selectItems;
        MainFormModel _formModel;
        ItemMenuScene itemMenu;

        public LuaEditerScene(MainGame game, MainFormModel formModel)
            : base(game)
        {
            this._formModel = formModel;
            game.input.handleKeyInputEvent += OnHandleInput;
            Initialize(game);
            OnCreat();
        }

        public MainFormModel MainFormModel { get { return _formModel; } }

        public override void OnCreat()
        {
            base.OnCreat();

            _mouseListener = new List<IMouseControl>();

            RectangleShape axieX, axieY;
            axieX = new RectangleShape();
            axieX.OnCreat();
            axieX.Color = Color.White;
            axieX.X = 400;
            axieX.Y = 240;
            axieX.Width = 800;
            axieX.Height = 1;
            axieX.OriginX = 400;
            axieX.OriginY = 0.5f;

            axieY = new RectangleShape();
            axieY.OnCreat();
            axieY.Color = Color.White;
            axieY.X = 400;
            axieY.Y = 240;
            axieY.Width = 1;
            axieY.Height = 480;
            axieY.OriginX = 0.5f;
            axieY.OriginY = 240;

            backGround.AttachChild(axieX);
            backGround.AttachChild(axieY);

            itemMenu = new ItemMenuScene(this.MainGame,MainFormModel);
            itemMenu.SetState("s01");
            itemMenu.LayerDepth = 0.8f;
            itemMenu.Visiable = false;

            _selectionManager = new SelectionManager();
            _selectionManager.OnSelectEntity += _formModel.OnSelectEntity;
            AddMouseListener(_selectionManager);
            AddMouseListener(itemMenu);

            RectangleShape test0 = new RectangleShape();
            test0.OnCreat();
            test0.Color = Color.Chocolate;
            
            test0.Name = "test0";
            test0.Width = 80;
            test0.Height = 80;
            test0.X = 500;
            test0.Y = 300;
            test0.OriginX = test0.Width / 2;
            test0.OriginY = test0.Height / 2;
            test0.LayerDepth = 0.5f;
            //AttachChild(test0);

            //_uiDecorators.Add();
            //AddEntityDecorator(s0);
            //RectangleShape s1 = new RectangleShape();
            //s1.OnCreat();
            //s0.AttachChild(s1);
            //s1.Color = Color.Aquamarine;
            //s1.Name = "s1";
            //s1.Rotation = 45;
            //s1.Origin = new Vector2(10, 10);
            //s1.SetSize(20, 20);
            //s1.SetPosition(0, 0);

            SortChildren();
            Camera.X = 400;
            Camera.Y = 240;
            //Camera.Rotation = 30;
        }

        public void AddMouseListener(IMouseControl mouserControl)
        {
            _mouseListener.Add(mouserControl);
        }

        //state xy is in screen coordinate
        public void OnMouseClick(MouseState state)
        {
            foreach (IMouseControl m in _mouseListener)
                m.OnMouseClick(state);
        }

        public void OnMouseDown(MouseState state)
        {
            foreach (IMouseControl m in _mouseListener)
                m.OnMouseDown(state);
        }

        public void OnMouseUp(MouseState state)
        {
            foreach (IMouseControl m in _mouseListener)
                m.OnMouseUp(state);
        }

        public void OnMouseMove(MouseState state)
        {
            foreach (IMouseControl m in _mouseListener)
                m.OnMouseMove(state);
        }

        public override void OnDraw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            backGround.OnDraw(gameTime, spriteBatch);
            Children.OnDraw(gameTime, spriteBatch);
            _selectionManager.OnDraw(gameTime, spriteBatch);
            itemMenu.OnDraw(gameTime, spriteBatch);
        }

        public override void OnUpdate(GameTime gameTime)
        {
            base.OnUpdate(gameTime);
            _selectionManager.OnUpdate(gameTime);
            itemMenu.OnUpdate(gameTime);

        }

        public override void Build(IsceneBuilder modeler, LuaTable define)
        {
            base.Build(modeler, define);
            foreach (BaseEntity child in Children)
            {
                _selectionManager.AddEntityDecorator(new SelectDecorator(child));
                _selectionManager.AddEntityDecorator(new DragDecorator(child,_formModel));
            }
        }

        //public override void AttachChild(IEntity child)
        //{
        //    CommandDecorator commandD = new CommandDecorator((BaseEntity)child);
        //    base.AttachChild(commandD);
        //}

        //public override void DetachChild(IEntity child)
        //{
        //    base.DetachChild(child);
        //}

        public bool IsOpenItemMenu
        {
            get { return itemMenu.Visiable; }
            set { itemMenu.Visiable = value; }
        }

        

    }
}

